﻿#define  FormySales


using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Text;
using System.Xml;
using System.Collections.Generic;
using System.Web.Script.Serialization;



namespace SyncServer
{
    internal class DownloadModules
    {
        internal DownloadModules() {}

        /// <summary>
        /// 生成下载JSON内容
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="FiledList"></param>
        /// <returns></returns>
        internal static String GeneractorContentToJson(System.Data.IDataReader reader,string tablename)
        {
            string jsonstring = "";
            if (reader != null)
            {
                try
                {
                    StringBuilder JsonString = new StringBuilder();
                    decimal timespan = 0;
                    List<string> JaggedArray = new List<string>();
                    while (reader.Read())
                    {
                        JsonString.Append("{ ");
                        for (int j = 0; j < reader.FieldCount; j++)
                        {

                            if (reader.GetName(j).ToString().ToUpper().Contains("LAST_UPDATE_TIME"))//获取后更新时间为时间戳返回
                            {
                                decimal d = 0;
                                decimal.TryParse(reader.GetValue(j).ToString(), out d);
                                if (d > timespan)
                                    timespan = d;
                            }else
                                JsonString.Append("\"" + reader.GetName(j).ToString() + "\":" + "\"" + reader.GetValue(j) + "\",");
                          
                            
                           
                        }
                        if (JsonString.ToString().EndsWith(","))
                            JsonString=JsonString.Remove(JsonString.Length - 1, 1);
                        JsonString.Append("},");
                    }
                    if (JsonString.ToString().EndsWith(","))
                        JsonString = JsonString.Remove(JsonString.Length - 1, 1);//去除末尾逗号
                    jsonstring = setJsonData(JsonString.ToString(), timespan, tablename, true, "", true);
                    //jsonstring = jsonstring.Replace("\n","");
                    //jsonstring = jsonstring.Replace("\t", "");
                }
                catch
                {
                    throw;
                }
                finally
                {
                    reader.Close();
                    GC.Collect();
                }

              
            }
            return jsonstring;
        }

       

        internal static string setJsonData(string rowdata, decimal timespan, string tablename, bool success, string errmsg,bool flag)
        {
            string jsonstring = "";
            StringBuilder JsonString = new StringBuilder();
            JsonString.Append("\"row\":[");
            JsonString.Append("{0}],");
            JsonString.Append("\"timespan\":\"{1}\",");//返回最新时间戳
            JsonString.Append("\"tablename\":\"{2}\",");//表名
            JsonString.Append("\"success\":\"{3}\",");//状态
            JsonString.Append("\"errmsg\":\"{4}\"");//错误信息
            jsonstring = string.Format(JsonString.ToString(), rowdata, timespan, tablename, success, errmsg);
            //if (flag)
                jsonstring = "{" + jsonstring + "},";
            //else
                //jsonstring = "[{" + jsonstring + "}]";
            return jsonstring;
        }
    }
}
